import store from '@/store'
import axios from 'axios'
import { Toast } from 'vant'
const axiosInstance = axios.create({
    baseURL: 'http://home.softeem.xin:9544',
    timeout: 5000 //5s内返回数据
})

//请求拦截器
axiosInstance.interceptors.request.use(req => {
    if (store.state.token !== null) {
        req.headers.softeem_ele_client_token = store.state.token;
    }
    return req
})

//响应拦截器
axiosInstance.interceptors.response.use(resp => {
    if (resp.data.status == 'success') {
        return Promise.resolve(resp.data.data)
    } else {
        //逻辑上的错误
        //清空所有的提示
        Toast.clear()
        Toast.fail(resp.data.msg);
        console.log('服务器请求是成功的，但是返回的结果不是我想要的');
        return Promise.reject(resp.data)
    }
}, error => {
    Toast.clear();
    Toast.fail('加载失败');
    return Promise.reject(error)
})
export default axiosInstance;